local dx_logger = require("dx_logger")
local dx_map = require("dx_map")
local dx_json = require("dx_json")
local dx_queue = require("dx_queue")

local mylog = {}

-- 获取全局配置，用于初始化日志参数
-- local g_config_str = dx_map.get(_G_map, "g_config_str")
-- _G.g_config = dx_json.decode(g_config_str)

-- -- 日志打印是否使用颜色
-- dx_logger.usecolor = g_config.usecolor
-- -- 输出日志文件路径
-- dx_logger.outpath = g_config.outpath
-- -- 日志等级（trace,debug,info,warn,error,fatal）
-- dx_logger.level = g_config.level
-- -- 最大日志存储量为max_size*2 字节,缺省是500k*2，大概1m
-- dx_logger.max_size = g_config.max_size

-- 日志打印是否使用颜色
dx_logger.usecolor = true
-- 输出日志文件路径
dx_logger.outpath = "/dx_cc101_app/data/"
-- 日志等级（trace,debug,info,warn,error,fatal）
dx_logger.level = "trace"
-- 最大日志存储量为max_size*2 字节,缺省是500k*2，大概1m
dx_logger.max_size = 512000

local modes = {
    {name = "trace"},
    {name = "info"},
    {name = "debug"},
    {name = "warn"},
    {name = "error"},
    {name = "fatal"}
}

local round = function(x, increment)
    increment = increment or 1
    x = x / increment
    return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment
end

local _tostring = tostring

local tostring = function(...)
    local t = {}
    for i = 1, select("#", ...) do
        local x = select(i, ...)
        if type(x) == "number" then
            x = round(x, .01)
        end
        t[#t + 1] = _tostring(x)
    end
    return table.concat(t, " ")
end

for i, x in ipairs(modes) do
    mylog[x.name] = function(...)
        dx_logger[x.name](...)
        mylog.log_report(i, ...)
    end
end

-- 开启调试模式上送日志
function mylog.log_report(i, ...)
    local level = i - 1
    local time = os.time()
    local log = tostring(...)
    local queue_msg = "20220316/event/log####" .. "level@!@" .. level .. "@#@time@!@" .. time .. "@#@log@!@" .. log
    dx_queue.push(mqtt_queue, queue_msg, 500)
end

return mylog
